var vcVar   = {
    _epage : 1,
}
var vcFunction = vcFunction || {
    fnGetDomain:function(url){
        return (url.match(/:\/\/(.[^/]+)/)[1]).replace('www.','');
    },
    fnOverlay:function(action){
        if(action == 'show'){
            $('html').addClass('ov');
            $('#lb_popup').removeClass('hidden').animate({
                opacity:1
            },500);
        }
        if(action == 'hide'){
            $('html').removeClass('ov');
            $('#lb_popup').html('').addClass('hidden').css({
                opacity:0
            });
        }
    },
    fnBgLightbox:function(action){
        if(action == 'show'){
            $('html').addClass('ov');
            $('#bg_lightbox').removeClass('hidden').animate({
                opacity:1
            },500);
        }
        if(action == 'hide'){
            $('html').removeClass('ov');
            $('#bg_lightbox').addClass('hidden').css({
                opacity:0
            });
        }
    },
    fnCheckEmailValid:function(email){
        var checkTLD=1;
        var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
        var emailPat=/^(.+)@(.+)$/;
        var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
        var validChars="\[^\\s" + specialChars + "\]";
        var quotedUser="(\"[^\"]*\")";
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
        var atom=validChars + '+';
        var word="(" + atom + "|" + quotedUser + ")";
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
        var matchArray=email.match(emailPat);
    
        if (matchArray==null) { //check if it conforms to typical patterns
        return false;
        }
        var user=matchArray[1];
        var domain=matchArray[2];
        for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {   //invalid chars in emailname
        return false;
           }
        }
        for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) { //invalid chars in domain name
        return false;
           }
        }
    
        var atomPat=new RegExp("^" + atom + "$");
        var domArr=domain.split(".");
        var len=domArr.length;
        for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
        return false;
           }
        }
        if (checkTLD && domArr[domArr.length-1].length!=2 && 
        domArr[domArr.length-1].search(knownDomsPat)==-1) {
        return false;
        }
    
        if (len<2) {        //missing a hostname
        return false;
        }
        
        return true;
    },
    fnFullScreen:function(){
        element = document.body;
        // Supports most browsers and their versions.
        var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
        if (requestMethod) { // Native full screen.
            requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            }
        }
    },
    getXOffset:function(){
        var pageX;
        if(typeof(window.pageXOffset)=='number') {
           pageX=window.pageXOffset;
        }
        else {
           pageX=document.documentElement.scrollLeft;
        }
        return pageX;
    },
    getYOffset:function(){
        var pageY;
        if(typeof(window.pageYOffset)=='number') {
           pageY=window.pageYOffset;
        }
        else {
           pageY=document.documentElement.scrollTop;
        }
        return pageY;
    },
    getOffset:function(){
        var offset = {};
        if(typeof(window.pageYOffset)=='number') {
           offset.pageX=window.pageXOffset;
           offset.pageY=window.pageYOffset;
        }
        else {
           offset.pageX=document.documentElement.scrollLeft; 
           offset.pageY=document.documentElement.scrollTop;
        }
        return offset;
    },
    goPage:function(a){
        page = $(a).closest('.go_page').find('.num_of_page').val() ? $(a).closest('.go_page').find('.num_of_page').val() : 1;
        url = $(a).attr('data-url');
        window.location.href = url+',p'+page;
    }
}
var vcCommon = vcCommon || {
    orgTitle: document.title,
    AJAX_TIMEOUT: 10000,
    
    
    ajaxPreloader:function(a,option){
        _opt = {
            top:'48%',
            left:'50%'
        }
        $.extend(_opt, option);
        if(a=='show')
            $('.ajax_loading').removeClass('hidden').css({top:_opt.top, left:_opt.left});
        else if(a=='hide')
            $('.ajax_loading').addClass('hidden');
    },
    //Các hàm check dữ liệu - so sánh định dạng dữ liệu
    checkData:function(a,b){
        for(var key in a){
            if(!b.hasOwnProperty(key)){
                return false;
            }
        }
        for(var key in b){
            if(!a.hasOwnProperty(key)){
                return false;
            }
        }
        return true;
    },
    checkDataChild:function(parent,child){
        for(var key in child){
            if(!parent.hasOwnProperty(key)){
                return false;
            }
        }
        return true;
    },
    
    getMsgChating:function(id){
        vcCommon.loadAjax('getMsg',{id:id},'json',function(resp){
            for(i in resp){
                var msg = '<div class="msg_item" data-msg-id='+resp[i].cha_id+'>';
                msg += '<span class="colorbase">'+resp[i].use_login+'</span> : ';
                msg += '<span class="muted">'+resp[i].cha_content+'</span>';
                msg +='</div>';
                $('.formchat_msg').prepend(msg);
            }
        })
    },
    
    closeLbChat:function(){
        $('.overlay').addClass('hidden');
        $('.popup').addClass('hidden');
    },
    
    loadAjax: function(method,data,type,success,beforeSend,failure){
        var failed = false; 
		/**
 * timerError = setTimeout(function(){
 * 			alert('Có l?i x?y ra. Vui lòng th? l?i sau.', 'Thông báo');
 * 			failure();
 * 		}, vcCommon.AJAX_TIMEOUT);
 */
        timerError = 0;
        $.ajax({
            type:'post',
            url:'/ajax/'+method,
            data:data,
            dataType:type,
            success:function(resp){
                if(type == 'json'){
                    if (resp.err == -10 && resp.data ){
    					vcVar.ajaxToken = resp.data;
    					clearTimeout(timerError);
    					return vcCommon.loadAjax(method, data, type, success, beforeSend, failure);
    				}
    				if(failed)
    					return;
    				clearTimeout(timerError);
    				if(success)
    					success(resp);
                }
                else{
                    clearTimeout(timerError);
    				if(success)
    					success(resp);
                }
            },
            beforeSend:function(){
                if(beforeSend)
                    beforeSend();
            }
        });
    },//End loadAjax
    loadPjax:function(){
        try{
            $('a[data-pjax]').pjax({
                scrollTo:0
            });
        }catch(err){
            
        }
    },
    
    
    showRegisterHome:function(){
        $('.banner_popup').removeClass('hidden');
        $('.ajaxload').removeClass('hidden');
        timeOut = setTimeout(function(){$('.ajaxload').addClass('hidden')},1000);
        //$('.ajaxload').addClass('hidden');
    },
    
    
};